//
//  SWUserAccount.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/2/28.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit

class SWUserAccount: NSObject {
    
    ///用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    ///access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            //为到期日属性赋值  timeIntervalSinceNow 现在开始数起的年
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    ///当前授权用户的UID。
    var uid: String?
    ///判断授权时间 是否过期
    var expiresDate: NSDate?
    ///用户昵称
    var screen_name: String?
    
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //MARK: ----- 归档 解档
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    init(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    override var description: String {
        // 当计算性属性只有get方法时,可以省略
        //        get {
        //        }
        let key = ["access_token", "expires_in", "expiresDate", "uid", "screen_name", "avatar_large"]
        
        return "UserAccount模型: \(dictionaryWithValuesForKeys(key).description)"
    }
    
}
